package com.gjy.work.alipay.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.gjy.work.alipay.properties.AliPayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 电脑网站支付
 *
 * @author gjy
 * @version 1.0
 * @since 2025-05-09 08:38:11
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {
    private static final Logger log = LoggerFactory.getLogger(AliPayController.class);

    @Resource
    private AlipayConfig aliPayConfig;
    @Resource
    private AliPayProperties aliPayProperties;

    private static final String subject = "订单主题: Mac笔记本";
    private static final String orderNo = "daniel82AAAA000032333361X01";
    private static final String totalAmount = "19999";

    @PostMapping("/pay")
    public void pay(@RequestBody Map<String, String> req) throws Exception {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig);

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(aliPayProperties.getNotifyUrl());
        request.setReturnUrl(aliPayProperties.getReturnUrl());
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        // 设置订单总金额
        model.setTotalAmount(req.get("amount"));
        // 设置订单标题
        model.setSubject(req.get("subject"));
        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setOutTradeNo(req.get("outTradeNo"));

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

//        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

    }

    @PostMapping("/query")
    public String query(@RequestBody Map<String, String> req) throws Exception {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig);
        // 构造请求参数以调用接口
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();

        // 设置订单支付时传入的商户订单号
        model.setOutTradeNo(req.get("outTradeNo"));
        // 设置支付宝交易号
        // model.setTradeNo("2014112611001004680073956707");

        // 设置查询选项
        List<String> queryOptions = new ArrayList<>();
        queryOptions.add("trade_settle_info");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }

    @GetMapping("/returnUrl")
    public void returnUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();

        log.info("returnUrl");
        log.info("returnUrl: {}", parameterMap);
    }

    @PostMapping("/notifyUrl")
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, String[]> parameterMap = request.getParameterMap();

        log.info("notify");
        log.info("notify: {}", parameterMap);

        response.setContentType("text/plain;charset=utf-8");
        response.getWriter().print("success");
    }

    @PostMapping("/refund")
    public String refund() throws Exception {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig);

        // 构造请求参数以调用接口
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();

        // 设置商户订单号
        model.setOutTradeNo("dfsdf1232123d");
        // 设置支付宝交易号
        model.setTradeNo("2025050922001436570506041779");

        // 设置退款金额
        model.setRefundAmount("71.00");

        // 设置退款原因说明
        model.setRefundReason("正常退款");

        // 设置退款请求号
        model.setOutRequestNo("HZ01RF001");

        // 设置查询选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("refund_detail_item_list");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);

        AlipayTradeRefundResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

        return response.getBody();
    }

    @PostMapping("/refundQuery")
    public String refundQuery() throws Exception {
        /// 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig);

        // 构造请求参数以调用接口
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();

        // 设置支付宝交易号
        model.setTradeNo("2025050922001436570506041779");

        // 设置商户订单号
        model.setOutTradeNo("dfsdf1232123d");

        // 设置退款请求号
        model.setOutRequestNo("HZ01RF001");

        // 设置查询选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("refund_detail_item_list");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);

        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }

    @PostMapping("/billDownload")
    public String billDownload() throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient(aliPayConfig);
        AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
        AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
        model.setBillType("trade");
        model.setBillDate("2016-04-05");
        request.setBizModel(model);
        AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response.getBody();
    }
}
